package com.manage.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ObjectUtil {

    public static void clone(Object source, Object target, String... ignoreProperties) {
        BeanUtils.copyProperties(source, target, ignoreProperties);
    }

    public static <T> T clone(Object source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        T t = null;
        try {
            t = targetClass.newInstance();
            clone(source, t);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return t;
    }

    public static <T> List<T> cloneList(Collection<? extends Object> list, Class<T> targetClass) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<T> result = new ArrayList<T>(list.size());
        try {
            for (Object d : list) {
                T t = targetClass.newInstance();
                clone(d, t);
                result.add(t);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

}
